package util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class PropertiesUtil {

	private static Properties properties;
	private static String path="config/application.properties";

	public static Properties initProperties() {
		if (properties == null) {
			properties = new Properties();
		}

		try (InputStream in = new FileInputStream(path);
				InputStreamReader reader = new InputStreamReader(in);) {
			properties.load(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return properties;
	}

	public static String readByKey(String key) {
		if (properties == null) {
			synchronized (PropertiesUtil.class) {
				if (properties == null) {
					initProperties();
				}
			}
		}
		return properties.getProperty(key);
	}
	public static String readByKey(String key, String defaultValue) {
		if (properties == null) {
			synchronized (PropertiesUtil.class) {
				if (properties == null) {
					initProperties();
				}
			}
		}
		return properties.getProperty(key, defaultValue);
	}



	public static void setValue(String key, String value) throws IOException {
		initProperties();
		properties.setProperty(key, value);
	}

	public static void main(String[] args) throws IOException {
		// String value = readByKey("driverClassName");
		// setValue("deviceCode","12423");
	}

}
